package logic

import (
	"context"
	"fmt"
	"project/model/mysql"

	"project/projectrpc/internal/svc"
	"project/projectrpc/projectrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *projectrpc.UpdateRequest) (*projectrpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line

	var shop mysql.Shop
	mysql.DB.Where("tel=?", in.Tel).First(&shop)

	var map1 = make(map[string]interface{})
	map1["name"] = in.Name
	map1["price"] = in.Price
	map1["tel"] = in.Tel
	err := mysql.DB.Model(&mysql.Shop{}).Where(&shop).Updates(&map1).Error
	if err != nil {
		return nil, fmt.Errorf("修改失败")
	}
	return &projectrpc.UpdateResponse{Id: int64(shop.ID)}, nil
}
